package com.hz.ruoyilgorder.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {


    //正常的队列
    public static final String NORMAL_QUEUE = "normal_queue";
    //正常的交换机
    public static final String NORMAL_EXCHANGE = "normal_exchange";
    //延时队列
    public static final String DELAY_QUEUE = "delay_queue";
    //延时交换机
    public static final String DELAY_EXCHANGE = "delay_exchange";



    // 定义一个队列  canal.queue
    @Bean
    public Queue canalQueue() {
        return new Queue("canal.queue", true);
    }



    @Bean
    public Queue normalQueue() {
        Map<String, Object> argsMap = new HashMap<String, Object>();
        //通过设置 x-dead-letter-exchange 和 x-dead-letter-routing-key
        // 来指定当队列中的消息无法被消费或过期时，应该被转发到哪个交换机以及使用哪个路由键。

        argsMap.put("x-dead-letter-exchange", DELAY_EXCHANGE);
        argsMap.put("x-dead-letter-routing-key", DELAY_QUEUE);
        // 创建正常队列
        return new Queue(NORMAL_QUEUE, true, false, false,argsMap);
    }
    @Bean
    public DirectExchange normalExchange() {
        // 创建正常队列交换机
        return new DirectExchange(NORMAL_EXCHANGE);
    }
    @Bean
    public Binding normalQueueBindingExchange() {
        // 将正常队列绑定到正常队列交换机上
        return BindingBuilder.bind(normalQueue()).to(normalExchange()).with(NORMAL_QUEUE);
    }


    @Bean
    public Queue delayQueue() {


        // 创建死信队列
        return new Queue(DELAY_QUEUE, true, false, false);
    }

    @Bean
    public DirectExchange delayExchange() {
        // 创建死信交换机
        return new DirectExchange(DELAY_EXCHANGE);
    }


    @Bean
    public Binding delayQueueBindingExchange() {
        // 将死信队列绑定死信交换机上
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(DELAY_QUEUE);
    }



}
